home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / demos / 108 / modula / maketoke.mod < prev    next >
Encoding:
Modula Implementation  |  1987-02-20  |  5.8 KB  |  197 lines

  1. IMPLEMENTATION MODULE MakeToken;
  2.  
  3.   (*
  4.    * MAKEMAKE.  Create a MAKEFILE for a MODULA-2 program.
  5.    *
  6.    * Written by Steve Tynor, 30 September 1986.
  7.    *            UUCP  : tynor@gitpyr
  8.    *            USNAIL: 2550 Akers Mill Rd. T-2, Atlanta GA. 30339
  9.    *
  10.    * Permission is granted to distribute, copy and change this program as long
  11.    * as this notice remains...
  12.    *)
  13.  
  14. IMPORT Streams;
  15.  
  16. VAR 
  17.   currentCh : CHAR;
  18.   currentStream : Streams.Stream;
  19.   
  20.   (*======================================================================*)
  21.   MODULE StreamStack;
  22.   IMPORT Streams;
  23.   EXPORT Pop, Push;
  24.  
  25.   CONST
  26.     StackSize = 20;
  27.   VAR
  28.     StrStack  : ARRAY [1 .. StackSize] OF Streams.Stream;
  29.     CharStack : ARRAY [1 .. StackSize] OF CHAR;
  30.     StackPtr  : CARDINAL;
  31.  
  32.     (*--------------------------------------------------------------------*)
  33.     PROCEDURE Push (str : Streams.Stream; ch : CHAR);
  34.     BEGIN
  35.       IF StackPtr + 1 <= StackSize THEN
  36.         INC (StackPtr);
  37.         StrStack [StackPtr] := str;
  38.         CharStack[StackPtr] := ch;
  39.       END; (* IF *)
  40.     END Push;
  41.  
  42.     (*--------------------------------------------------------------------*)
  43.     PROCEDURE Pop (VAR str : Streams.Stream; VAR ch : CHAR);
  44.     BEGIN
  45.       IF StackPtr > 0 THEN
  46.         str := StrStack [StackPtr];
  47.         ch  := CharStack[StackPtr];
  48.         DEC (StackPtr);
  49.       END; (* IF *)
  50.     END Pop;
  51.  
  52.   BEGIN
  53.     StackPtr := 0;
  54.   END StreamStack;
  55.   (*======================================================================*)
  56.  
  57.  
  58.   (*----------------------------------------------------------------------*)
  59.   PROCEDURE OpenFile (VAR filename : ARRAY OF CHAR;
  60.                       VAR success  : BOOLEAN);
  61.   VAR
  62.     result : INTEGER;
  63.   BEGIN
  64.     Push (currentStream, currentCh);
  65.     Streams.OpenStream(currentStream, filename, Streams.READ, result);
  66.     currentCh := ' ';
  67.     success := result = 0;
  68.   END OpenFile;
  69.  
  70.   (*----------------------------------------------------------------------*)
  71.   PROCEDURE CloseFile;
  72.   VAR
  73.     result : INTEGER;
  74.   BEGIN
  75.     Streams.CloseStream(currentStream, result);
  76.     Pop (currentStream, currentCh);
  77.   END CloseFile;
  78.  
  79.  
  80.   (*----------------------------------------------------------------------*)
  81.   PROCEDURE SeparatingChar (ch : CHAR) : BOOLEAN;
  82.     (* we're just worried about a subset of the real separating chars... *)
  83.   BEGIN
  84.     RETURN NOT (((ORD(ch) >= ORD('a')) AND (ORD(ch) <= ORD('z'))) OR
  85.                 ((ORD(ch) >= ORD('A')) AND (ORD(ch) <= ORD('Z'))) OR
  86.                 ((ORD(ch) >= ORD('0')) AND (ORD(ch) <= ORD('9'))));
  87.   END SeparatingChar;
  88.  
  89.  
  90.   (*----------------------------------------------------------------------*)
  91.   PROCEDURE NextToken (VAR token : ARRAY OF CHAR;
  92.                        VAR eof   : BOOLEAN);
  93.   VAR
  94.     c : CARDINAL;
  95.   BEGIN
  96.     c := 0;
  97.     IF currentCh = ';' THEN
  98.       token[0] := ';';
  99.       token[1] := 0C;
  100.       eof := Streams.EOS(currentStream);
  101.       Streams.Read8Bit(currentStream, currentCh);
  102.       RETURN;
  103.     END; (* IF *)
  104.     WHILE SeparatingChar (currentCh) DO
  105.       Streams.Read8Bit(currentStream, currentCh);
  106.       IF currentCh = ';' THEN
  107.         token[0] := ';';
  108.         token[1] := 0C;
  109.         eof := Streams.EOS(currentStream);
  110.         Streams.Read8Bit(currentStream, currentCh);
  111.         RETURN;
  112.       ELSIF currentCh = '(' THEN
  113.         token[c] := currentCh;
  114.         Streams.Read8Bit(currentStream, currentCh);
  115.         IF currentCh = '*' THEN
  116.           SkipComment;
  117.         ELSE
  118.           token[0] := '(';
  119.           token[1] := currentCh;
  120.           c := 2;
  121.         END; (* IF *)
  122.       END; (* IF *)
  123.     END; (* WHILE *)
  124.     REPEAT
  125.       IF currentCh = '(' THEN
  126.         token[c] := currentCh;
  127.         Streams.Read8Bit(currentStream, currentCh);
  128.         IF currentCh = '*' THEN
  129.           IF c > 0 THEN
  130.             token[c+1] := 0C;
  131.             SkipComment;
  132.             RETURN;
  133.           ELSE
  134.             SkipComment;
  135.           END; (* IF *)
  136.         END; (* IF *)
  137.       END; (* IF *)
  138.       token[c] := currentCh;
  139.       INC (c);
  140.       Streams.Read8Bit(currentStream, currentCh);
  141.     UNTIL Streams.EOS(currentStream) OR 
  142.           (c > HIGH (token)) OR 
  143.           SeparatingChar (currentCh);
  144.     token[c] := 0C;
  145.     eof := Streams.EOS (currentStream);
  146.   END NextToken;
  147.  
  148.  
  149.   (*----------------------------------------------------------------------*)
  150.   PROCEDURE SkipTillSemicolon (VAR eof   : BOOLEAN);
  151.   VAR
  152.     ch : CHAR;
  153.   BEGIN
  154.     REPEAT
  155.       IF (NOT Streams.EOS(currentStream)) AND (currentCh = '(') THEN
  156.         Streams.Read8Bit(currentStream, currentCh);
  157.         IF (NOT Streams.EOS (currentStream)) AND (currentCh = '*') THEN
  158.           SkipComment;
  159.         END; (* IF *)
  160.       ELSE
  161.         Streams.Read8Bit(currentStream, currentCh);
  162.       END; (* IF *)
  163.     UNTIL Streams.EOS (currentStream) OR (currentCh = ';');
  164.   END SkipTillSemicolon;
  165.  
  166.  
  167.   (*----------------------------------------------------------------------*)
  168.   PROCEDURE SkipComment;
  169.   VAR
  170.     level : CARDINAL;
  171.     done  : BOOLEAN;
  172.   BEGIN
  173.     level := 1;
  174.     Streams.Read8Bit(currentStream, currentCh);
  175.     done := FALSE;
  176.     REPEAT
  177.       IF (NOT Streams.EOS(currentStream)) AND (currentCh = '*') THEN
  178.         Streams.Read8Bit(currentStream, currentCh);
  179.         IF (NOT Streams.EOS(currentStream)) AND (currentCh = ')') THEN
  180.           DEC(level);
  181.           done := level = 0;
  182.         END; (* IF *)
  183.       ELSIF (NOT Streams.EOS(currentStream)) AND (currentCh = '(') THEN
  184.         Streams.Read8Bit(currentStream, currentCh);
  185.         IF (NOT Streams.EOS (currentStream)) AND (currentCh = '*') THEN
  186.           INC(level);
  187.         END; (* IF *)
  188.       ELSE
  189.         Streams.Read8Bit(currentStream, currentCh);
  190.       END; (* IF *)
  191.     UNTIL Streams.EOS(currentStream) OR done;
  192.   END SkipComment;
  193.  
  194. BEGIN
  195.   currentCh := ' ';
  196. END MakeToken.
  197.